//
//  ZZCcurrentController.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/23.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCcurrentController.h"
#import "ZZCCurrentDetailsController.h"
#import "ZZCBIGMenuView.h"
#import "ZZCDueController.h"
#import "ZZCOwnPropertyController.h"
#import "ZZCLookPayPasswordController.h"
#import "ZZCMeUbiViewController.h"

@interface ZZCcurrentController ()

@property (strong, nonatomic) GRBkeyTextField * safeTextField;
@property (strong, nonatomic) GRBkeyTextField * safeInputTextField;

@property (strong, nonatomic) GRBsafeKeyBoard * board;

@property (weak,   nonatomic) IBOutlet UIButton *lifeTimeBtn;
@property (strong, nonatomic) ZZCBIGMenuView *menuView;

@property (retain, nonatomic) NSMutableArray *terminalInfoArr;
@property (retain, nonatomic) NSMutableArray *regularIDArr;

@property (copy,   nonatomic) NSString *regularID;
@property (copy,   nonatomic) NSString *PWD;
//@property (assign, nonatomic) BOOL isRight;
@end

@implementation ZZCcurrentController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"U币转活期";
    _terminalInfoArr = [NSMutableArray new];
    _regularIDArr = [NSMutableArray new];
    [self requestData];
    // Do any additional setup after loading the view.
    IQKeyboardManager *iq = [IQKeyboardManager sharedManager];
    iq.enableAutoToolbar = NO;

}
- (void)requestData {
    NSString * token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"token":token};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,
                            @"token":token};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_MYREGULAR parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"成功-%@",responseObject);
            NSArray *array = responseObject[@"data"][@"list"];
            for (NSDictionary *dict in array) {
                [_regularIDArr addObject:dict[@"id"]];
                [_terminalInfoArr addObject:[NSString stringWithFormat:@"%@元(%@个月/利率%@%%)",dict[@"money"],dict[@"cycle"],dict[@"rate"]]];
            }
        }
    }];
}
- (IBAction)lifeTimeClickAction:(UIButton *)sender {
        ZZCLog(@"选择存期");
    if (_terminalInfoArr.count == 0) {
        [SVProgressHUD showInfoWithStatus:@"无法转活期，没有可用金额"];
        return;
    }
        [self.menuView removeFromSuperview];
        self.menuView = [[ZZCBIGMenuView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.lifeTimeBtn.frame), ZZCScreenW, 132) contentArray:_terminalInfoArr];
        __weak typeof (ZZCBIGMenuView *) weakSelf = self.menuView;
        __weak typeof (self) weakself = self;
        self.menuView.block = ^(BOOL isSeletct ,NSString *title){
            [weakSelf removeFromSuperview];
            if (title.length != 0) {
                [weakself.lifeTimeBtn setTitle:title forState:0];
                [weakself.lifeTimeBtn setImage:[UIImage new] forState:0];
                NSInteger Index  = [weakself.terminalInfoArr indexOfObject:title];
                weakself.regularID = weakself.regularIDArr[Index];
            }
        };
    //
        [self.view addSubview:self.menuView];

}

//确定
- (IBAction)confirmClick {
    self.board = [GRBsafeKeyBoard GRB_showSafeInputKeyBoard];
    __weak typeof(self)weakSelf = self;
    self.board.GRBsafeKeyFinish=^(NSString * passWord){
        if (passWord.length==6) {
            _PWD = passWord;
            [weakSelf.board GRB_endKeyBoard];
            ZZCLog(@"你输入的密码为---%@",passWord);
            [weakSelf submitRequest];
        }
    };
    self.board.GRBsafeKeyClose=^{
        
        [weakSelf.board GRB_endKeyBoard];
    };
    
    self.board.GRBsafeKeyForgetPassWord=^{
        ZZCLog(@"点击忘记密码了");
        ZZCLookPayPasswordController *VC = [ZZCLookPayPasswordController new];
        [weakSelf.navigationController pushViewController:VC animated:YES];

    };

}
- (void)submitRequest {
    NSString * token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"token":token,
                           @"regular_id":self.regularID,
                           @"pay_password":_PWD
                           };
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,
                            @"token":token,
                            @"regular_id":self.regularID,
                            @"pay_password":_PWD
                            };
    NSLog(@"%@",param);
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_TURNOUT parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"成功-%@",responseObject);
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            if([responseObject[@"code"] isEqual:@(200)]){
                NSLog(@"U币转活期进行页面跳转");
                for (UIViewController *controller in self.navigationController.viewControllers) {
                    if ([controller isKindOfClass:[ZZCOwnPropertyController class]]||
                        [controller isKindOfClass:[ZZCMeUbiViewController class]]) {
                        [self.navigationController popToViewController:controller animated:YES];
                    }
                }

            }
        }
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
